package com.solotiger.douban.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.solotiger.douban.R;


/**
 * 封装参数, 简化调用,
 * 解决不断的显示Toast的问题
 */
public class ToastUtil {
    /**
     * 提示信息显示参数
     */
    public static enum Layout {
        TOP(Toast.LENGTH_SHORT, Gravity.TOP, 64),
        CENTER(Toast.LENGTH_SHORT, Gravity.CENTER, 0),
        BOTTOM(Toast.LENGTH_SHORT, Gravity.BOTTOM, 64),
        TOP_LONG(Toast.LENGTH_LONG, Gravity.TOP, 64),
        CENTER_LONG(Toast.LENGTH_LONG, Gravity.CENTER, 0),
        BOTTOM_LONG(Toast.LENGTH_LONG, Gravity.BOTTOM, 64),;
        // 默认值
        public static final Layout DEFAULT = CENTER;
        // 长时间显示默认值
        public static final Layout LONG = CENTER_LONG;

        public final int duration;
        public final int gravity;
        public final int yOffset;

        private Layout(int duration, int gravity, int yOffset) {
            this.duration = duration;
            this.gravity = gravity;
            this.yOffset = yOffset;
        }
    }

    /**
     * 使用默认值显示提示信息
     */
    public static void show(Context context, Exception e, int resIdForEmpty) {
        String msg = e.getMessage();
        if (msg == null || msg.trim().length() == 0) {
            show(context, resIdForEmpty, Layout.DEFAULT);
        } else {
            show(context, msg, Layout.DEFAULT);
        }
    }

    /**
     * 使用默认值显示提示信息
     */
    public static void show(Context context, int resId) {
        show(context, resId, Layout.DEFAULT);
    }

    /**
     * 使用默认值显示提示信息
     */
    public static void show(Context context, CharSequence text) {
        show(context, text, Layout.DEFAULT);
    }

    /**
     * 显示提示信息
     */
    public static void show(Context context, int resId, Layout layout) {
        show(context, resId, layout.duration, layout.gravity, layout.yOffset);
    }

    /**
     * 显示提示信息
     */
    public static void show(Context context, CharSequence text, Layout layout) {
        show(context, text, layout.duration, layout.gravity, layout.yOffset);
    }

    /**
     * 显示提示信息
     */
    public static void show(Context context, int resId, int duration, int gravity, int yOffset) {
        if (context != null) {
            String text = context.getString(resId);
            show(context, text, duration, gravity, yOffset);
        }
    }

    /**
     * 显示提示信息
     *
     * @param text     内容
     * @param duration 时长(Toast.LENGTH_SHORT, Toast.LENGTH_LONG)
     * @param gravity  对齐(Gravity.TOP, Gravity.CENTER, Gravity.BOTTOM)
     * @param yOffset  上下边距(dp)
     */
    public static void show(Context context, CharSequence text, int duration, int gravity, int yOffset) {
        if (text != null && text.length() > 0) {
            Toast toast = createToast(context);
            if (toast != null) {
                textView.setText(text);
                toast.setDuration(duration);
                if (yOffset != 0) {
                    int yDp = DisplayUtil.dip2px(context, yOffset);
                    toast.setGravity(Gravity.CENTER_HORIZONTAL | gravity, 0, yDp);
                } else {
                    toast.setGravity(Gravity.CENTER_HORIZONTAL | gravity, 0, 0);
                }
                toast.show();
            }
        }
    }

    // 单实例提示信息
    private static Toast mToast;

    // 获取提示信息实例
    @SuppressLint("ShowToast")
    private static Toast createToast(Context context) {
        if (mToast != null) {
            return mToast;
        } else {
            if (context != null) {
                createText(context, "", Toast.LENGTH_SHORT);
                return mToast;
            } else {
                return null;
            }
        }
    }

    private static View view;

    private static TextView textView;

    public static synchronized void createText(Context context, String msg, int length) {
        if(null != mToast) {
            return;
        }
        mToast = Toast.makeText(context, msg, length);
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.toast, null);
        textView = (TextView) view.findViewById(R.id.textview_toast);
//        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        lp.gravity = Gravity.CENTER;
//        view.setLayoutParams(lp);

//        textView.setText("dddddddddddddd");
        mToast.setView(view);


//        Toast toast = new Toast(context);
//        toast.setView(LayoutInflater.from(context).inflate(R.layout.toast, null));
//        toast.setText("toast use");
//// Button是否fill_parent是由gravity控制的, xml中的不起任何作用
//        toast.setGravity(toast.getGravity() | Gravity.FILL_HORIZONTAL,
//                toast.getXOffset(), toast.getYOffset());
//        toast.setDuration(Toast.LENGTH_SHORT);
//        toast.show();
    }
}
